package com.alinesno.cloud.monitor.agent.aspect;

import java.text.DecimalFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.alinesno.cloud.compoment.kafka.KafkaProducer;

/**
 * 基础监控对象工程对象 <br/>
 * @author WeiXiaoJin
 * @since 2019年12月13日 下午8:57:43
 */
public abstract class BaseAspect {

	protected static final Logger log = LoggerFactory.getLogger(BaseAspect.class) ; 

	public static final String WATCHER_TOPIC_METHOD_EXCEPTION = "watcher_topic_method_exception" ;  // 方法异常信息
	public static final String WATCHER_TOPIC_METHOD_RUNTIME = "watcher_topic_method_runtime" ;   // 方法运行时间 
	public static final String WATCHER_TOPIC_SYSTEM_MONITOR = "watcher_topic_system_monitor" ;   // 系统运行监控
	public static final String WATCHER_TOPIC_SQL = "watcher_topic_sql" ;   // 系统SQL运行监控
	public static final String WATCHER_TOPIC_WEB_CONTROLLER = "watcher_topic_web_controller" ;   // web监控
	
	public static final String WATCHER_STATUS_REPORT_RATE_TIME = "0/30 * * * * ?" ;  // 定时上报时间 

	@Autowired
	protected KafkaProducer kafkaProducer; 

	@Value("${spring.application.name:-1}")
	protected String applicationName ;

	/**
	 * 是否监控方法
	 */
	@Value("${alinesno.watcher.method:true}")
	protected boolean watcherMethod ; 
	
	/**
	 * 是否监控方法请求
	 */
	@Value("${alinesno.watcher.request-url:true}")
	protected boolean watcherRequestUrl ; 

	/**
	 * 是否监控请求SQL
	 */
	@Value("${alinesno.watcher.watcher:true}")
	protected boolean watcherSQL ; 
	
	/**
	 * 是否监控服务器资源
	 */
	@Value("${alinesno.watcher.monitor:true}")
	protected boolean watcherMonitor ; 
	
	/**
	 * 是否监控业务 
	 */
	@Value("${alinesno.watcher.business:true}")
	protected boolean watcherBusiness ; 

	/**
     * 计算结果百分比，保留1位小数
     *
     * @param a 除数
     * @param b 被除数
     * @return
     */
    protected static String calculateResultOfPercent(double a, Integer b) {
        if (b == 0) {
            return "0";
        }

        DecimalFormat df = new DecimalFormat("#.##");
        return df.format(a/b);
    }
}
